﻿using FBroSharp;
using FBroSharp.DataType;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BaseTest
{
    public class BrowserDis
    {
        //创建浏览器
        public static void CreatBrowser(string title, string url, IntPtr parent_window, int x, int y, int width, int height, string user_data_path, string user_flag = "")
        {
            //开始创建浏览器
            FBroSharpWindowsInfo windows_info = new FBroSharpWindowsInfo
            {
                parent_window = parent_window,
                x = x,
                y = y,
                width = width,
                height = height,
                window_name = title
            };

            FBroSharpRequestContext request_context = default;
            //创建浏览器事件回调类
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + $"设置前的user_data_path：{user_data_path}");
            BrowserEvent browser_event = new BrowserEvent();
            if (!string.IsNullOrEmpty(user_data_path))
            {
                //设置运行环境
                FBroSharpRequestContextSet contextSet = new FBroSharpRequestContextSet();
                contextSet.cache_folder = user_data_path;
                contextSet.persist_session_cookies = true; // 如果是在创建浏览器的时候设置屏蔽保存账号密码弹窗生效，需要设置persist_session_cookies,如果不设置一样弹

                request_context = (FBroSharpRequestContext)FBroSharpRequestContext.CreateContext(contextSet);
                Console.WriteLine(MethodBase.GetCurrentMethod().Name + "设置后的缓存目录:" + request_context.GetCachePath());
                FBroSharpValue FBvalue = new FBroSharpValue();

                FBvalue.SetBool(true);
                request_context.SetPreference("credentials_enable_autosignin", FBvalue);
                request_context.SetPreference("credentials_enable_service", FBvalue);
                Console.WriteLine(MethodBase.GetCurrentMethod().Name + "已设置首选项");

            }



            //设置不需要的事件
            //FBroSharpEventDisableControl event_contrl = new FBroSharpEventDisableControl();
            //event_contrl.disableGetAudioHandler = true;


            //设置传递参数
            //FBroSharpDictionaryValue extra_info = new FBroSharpDictionaryValue();//.Creat();
            //extra_info.SetString("传参测试", "这是创建浏览器的时候传递过来的参数");

            if (FBroSharpControl.CreatBrowser(url, windows_info, default, request_context /*FBroSharpRequestContext*/, default/*extra_info*/, browser_event, default/*event_contrl*/, user_flag))
                Console.WriteLine("true");
        }
    }
}
